package com.hspedu.newData.dmsxl.数组.前缀和;

import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-23 
 * @Version: 1.0
 */

public class LC560和为k的子数组 {


    // todo
    public int subarraySum(int[] nums, int k) {

        int slow = 0;
        int fast = 0;
        int target = 0;
        int count = 0;
        while(slow < nums.length){
            if (target < k){
                if (fast == nums.length){
                    return count;
                }
                target += nums[fast++];
            }else if (target > k){
                target -= nums[slow++];
            }else {
                count ++;
                target -= nums[slow++];
            }
        }
        return count;
    }

    @Test
    public void test() {
        // 测试代码
        int i = subarraySum(new int[]{1, 2, 3}, 3);
        System.out.println(i);

    }

}
